<?php 
/**
 * 后台用户管理
 * @author cjli
 *
 */
class ManageAction extends AdminAction
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function index()
	{
		$admindb = D('admin');
		$admindb->field('admin.*,admin_role.rolename');
		$admindb->join('admin_role ON admin_role.id = admin.roleid');
		$adminList = $admindb->select();
		$this->assign('adminList', $adminList);
		$this->display('admin_list');
	}
	
	public function add()
	{
		$admindb = D('admin');
		if(isset($_POST['dosubmit'])){
			if($_POST['info']['password'] !== $_POST['info']['pwdconfirm']){
				$this->error('两次输入密码不一致！', U('manage/add'), 'add');
			}
			unset($_POST['info']['pwdconfirm']);
			$_POST['info']['password'] = md5($_POST['info']['password']);
			$admindb->data($_POST['info']);
			$admindb->add();
			$this->success('添加成功！', U('manage/index'), 'add');
		}else{
			$roledb = D('admin_role');
			$roles = $roledb->where('display=1')->order('listorder ASC')->select();
			$this->assign('roles', $roles);
			$this->display('admin_add');
		}
	}
	
	public function edit()
	{
		$admindb = D('admin');
		if(isset($_POST['dosubmit'])){
			if($_POST['info']['password'] !== $_POST['info']['pwdconfirm']){
				$this->error('两次输入密码不一致！', U('manage/edit', 'userid='.$_POST['info']['id']), 'edit');
			}
			unset($_POST['info']['pwdconfirm']);
			if( empty($_POST['info']['password'])){
				unset($_POST['info']['password']);
			}else{
				$_POST['info']['password'] = md5(trim($_POST['info']['password']));
			}
			if($_POST['info']['id'] == 1){
				unset($_POST['info']['roleid']);
				unset($_POST['info']['display']);
			}
			$admindb->data($_POST['info']);
			$admindb->save();
			$this->success('修改成功！', U('manage/index'), 'edit');
		}else{
			$userid = intval($_GET['userid']);
			$user = $admindb->where('id='.$userid)->find();
			$roledb = D('admin_role');
			$roles = $roledb->where('display=1')->order('listorder ASC')->select();
			$this->assign('user', $user);
			$this->assign('roles', $roles);
			$this->display('admin_edit');
		}
	}
	
	public function delete()
	{
		$userid = intval($_GET['userid']);
		$admindb = D('admin');
		$admindb->delete($userid);
		$this->success('删除管理员成功！', U('manage/index'));
	}
	
	/**
	 * 我的面板-修改个人信息
	 */
	public function public_edit_info()
	{
		$user_id = $_SESSION['admin_user_info']['id'];
		$admindb = D('admin');
		if(isset($_POST['dosubmit'])){
			$data = array(
										'realname' => $_POST['info']['realname'], 
										'email' => $_POST['info']['email']
										);
			$rs = $admindb->where('id='.$user_id)->setField($data);
			$this->assign('jumpUrl', U('manage/public_edit_info'));
			if(false === $rs){
				$this->error('修改失败！');
			}else{
				$this->success('修改成功！');
			}
		}else{
			$user = $admindb->where('id='.$user_id)->find();
			$this->assign('user', $user);
			$this->display('edit_info');
		}
	}
	
	/**
	 * 我的面板-修改密码
	 */
	public function public_edit_pwd()
	{
		$userid = $_SESSION['admin_user_info']['id'];
		$admindb = D('admin');
		if(isset($_POST['dosubmit'])){
			$password = $admindb->where(array('id'=>$userid))->getField('password');
			if (md5($_POST['old_password']) != $password ) {
				$this->error('旧密码输入错误！', U('manage/public_edit_pwd'));
			}
			$rs = $admindb->where('id='.$user_id)->setField('password', $_POST['new_password']);
			$this->assign('jumpUrl', U('manage/public_edit_pwd'));
			$this->success('修改成功！', U('public/logout'));
		}else{
			$user = $admindb->where('id='.$user_id)->find();
			$this->assign('user', $user);
			$this->display('edit_pwd');
		}
	}
	
/**
	 * 异步检测用户名
	 */
	function public_checkname_ajx() {
		$username = isset($_GET['username']) && trim($_GET['username']) ? trim($_GET['username']) : exit(0);
		$admindb = D('admin');
		$id = $admindb->where(array('username'=>$username))->getField('id');
		if ($id){
			exit('0');
		}
		exit('1');
	}
	/**
	 * 异步检测密码
	 */
	function public_password_ajx() {
		$userid = $_SESSION['admin_user_info']['id'];
		$admindb = D('admin');
		$password = $admindb->where(array('id'=>$userid))->getField('password');
		if (md5($_GET['old_password']) == $password ) {
			exit('1');
		}
		exit('0');
	}
	/**
	 * 异步检测emial合法性
	 */
	function public_email_ajx() {
		$admindb = D('admin');
		$where['email'] = trim($_GET['email']);
		$where['id'] = array('neq', $_SESSION['admin_user_info']['id']);
		$id = $admindb->where($where)->getField('id');
		if(is_null($rs)){
			exit('1');
		}
		exit('0');
	}
	
}
?>